package cn.com.cfae.iras.doc.analyze.parse.model;

import cn.com.cfae.iras.doc.analyze.keys.ExtractMethod;
import cn.com.cfae.iras.doc.analyze.keys.UnitType;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class ExtractBizModel {


    public boolean isEnableAmbiguity() {
        return isEnableAmbiguity;
    }

    public void setEnableAmbiguity(boolean enableAmbiguity) {
        isEnableAmbiguity = enableAmbiguity;
    }

    public List<String> getAmbiguityWordList() {
        return ambiguityWordList;
    }

    public void setAmbiguityWordList(List<String> ambiguityWordList) {
        this.ambiguityWordList = ambiguityWordList;
    }

    List<String> ambiguityWordList = null;


    private boolean isEnableAmbiguity = false;

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    private String modelName;

    public ExtractMethod[] getExtractMethods() {
        return extractMethods;
    }

    public void setExtractMethods(ExtractMethod[] extractMethods) {
        this.extractMethods = extractMethods;
    }

    /**
     * 抽取方式列表，大小≥1，下标为0，为第一抽取方式
     */

    private ExtractMethod[] extractMethods;

    public List<ExtractItem> getExtractItemList() {
        return extractItemList;
    }

    public void setExtractItemList(List<ExtractItem> extractItemList) {
        this.extractItemList = extractItemList;
        extractItemList.forEach(e -> {
            extractItemMap.put(e.getItemName(), e);
            for (String synonym : e.getSynonyms()) {
                if (StringUtils.isEmpty(synonym)) {
                    continue;
                }
                if (isEnableAmbiguity && ambiguityWordList.contains(e.getItemName())) {
                    continue;
                }
                synonymExtractItemMap.put(synonym, e.getItemName());
            }
        });

        setSynonyms(Arrays.asList(synonymExtractItemMap.keySet().toArray(new String[]{})));
    }

    Map<String, ExtractItem> extractItemMap = new HashMap<>();

    public ExtractItem getExtractItem(String itemName) {
        return extractItemMap.get(itemName);
    }

    private List<ExtractItem> extractItemList;

    public List<String> getSynonyms() {
        return synonyms;
    }

    public void setSynonyms(List<String> synonyms) {
        this.synonyms = synonyms;
    }


    private Map<String, String> synonymExtractItemMap = new HashMap<>();


    public ExtractItem getExtractItemBySynonym(String synonym) {
        return extractItemMap.get(synonymExtractItemMap.get(synonym));
    }

    /**
     * 同义词
     */
    List<String> synonyms;

    /**
     * 获取近三年及一期的表格数据
     * 根据年份获取到Map，key为会计科目、extractitem的key为名称及值
     *
     * @return
     */
    public Map<String, ExtractItem> getTable(String year) {
        if (StringUtils.isEmpty(year)) {
            return null;
        }
//        if (!tableExtractItemMap.containsKey(year)) {
//            addTable(year);
//        }
        return tableExtractItemMap.get(year);
    }

    public List<Map<String, ExtractItem>> getTable2(String year) {
        if (StringUtils.isEmpty(year)) {
            return null;
        }
        if (!tableExtractItemMapList.containsKey(year)) {
            addTable2(year);
        }
        return tableExtractItemMapList.get(year);
    }

    /**
     * 财务三表、财务指标
     * 资产负债表、现金流量表、利润表
     * 偿债能力、营运能力、盈利能力
     *
     * @return
     */
    public Map<String, Map<String, ExtractItem>> getTables() {
        return tableExtractItemMap;
    }

    /**
     * 营业收入、营业成本、毛利润、毛利率
     *
     * @return
     */
    public Map<String, List<Map<String, ExtractItem>>> getTables2() {
        return tableExtractItemMapList;
    }

    public ExtractItem getTableExtractItem(String year, String extractItemName) {
        return getTable(year).get(extractItemName);
    }

    private Map<String, Map<String, ExtractItem>> tableExtractItemMap = new HashMap<>();

    public void addTableExtractItems(Map<String, Map<String, ExtractItem>> tableExtractItemMap) {
        tableExtractItemMap.putAll(tableExtractItemMap);
    }

    public void addTable(String year) {
        if (StringUtils.isEmpty(year)) {
            return;
        }
        if (!tableExtractItemMap.containsKey(year)) {
            tableExtractItemMap.put(year, new HashMap<String, ExtractItem>());
        }
    }


    public void addTableExtractItem(String year, ExtractItem extractItem) {
        if (StringUtils.isEmpty(year)) {
            return;
        }
        if (!tableExtractItemMap.containsKey(year)) {
            tableExtractItemMap.put(year, new HashMap<String, ExtractItem>());
        }
        tableExtractItemMap.get(year).put(extractItem.getItemName(), extractItem);
    }

    private Map<String, List<Map<String, ExtractItem>>> tableExtractItemMapList = new HashMap<>();

    private List<Map<String, ExtractItem>> tableExtractItemList = new ArrayList<>();

    /**
     * 董事、监事、高管
     *
     * @param extractItemMap
     */
    public void addTable3(Map<String, ExtractItem> extractItemMap) {
        tableExtractItemList.add(extractItemMap);
    }

    /**
     * 董、监、高
     *
     * @return
     */
    public List<Map<String, ExtractItem>> getTables3() {
        return tableExtractItemList;
    }

    public void addTable2(String year) {
        if (StringUtils.isEmpty(year)) {
            return;
        }
        if (!tableExtractItemMapList.containsKey(year)) {
            tableExtractItemMapList.put(year, new ArrayList<Map<String, ExtractItem>>());
        }
    }

    public void addTableExtractItem2(String year, Map<String, ExtractItem> extractItems) {
        addTable2(year);
        tableExtractItemMapList.get(year).add(extractItems);
    }

    public String getUnitType() {
        return unitType;
    }

    public void setUnitType(String unitType) {
        this.unitType = unitType;
    }

    private String unitType;


    public TextLayout getTableNameTextLayout() {
        return tableNameTextLayout;
    }

    public void setTableNameTextLayout(TextLayout tableNameTextLayout) {
        this.tableNameTextLayout = tableNameTextLayout;
    }

    private TextLayout tableNameTextLayout;


}
